import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;


public class VentanaDeConfiguracion extends JFrame implements ActionListener {

	private Appletinicio applet;
	private JComboBox comboMapas;
	private JButton boton;
	private JTextField ancho, alto, minas;
	private JLabel anchoL, altoL, minasL;
	private boolean expandido;


	public static final long serialVersionUID = 6;

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("aceptar")){
			switch (comboMapas.getSelectedIndex()){
			case 0: applet.setMapa(new Mapa(9, 9, 10)); break;
			case 1: applet.setMapa(new Mapa(16, 16, 40)); break;
			case 2: applet.setMapa(new Mapa(16, 30, 99)); break;
			case 3: applet.setMapa(new Mapa("centroVacio")); break;
			case 4: applet.setMapa(new Mapa("letraH")); break;
			case 5: 
				try {
					applet.setMapa(new Mapa (Integer.parseInt(ancho.getText()),
							Integer.parseInt(alto.getText()), Integer.parseInt(minas.getText())));
				} catch (NumberFormatException error) {
					ancho.setText("Introduzca un numero");
					alto.setText("Introduzca un numero");
					minas.setText("Introduzca un numero");
					return;
				}
			}
			applet.nuevoJuego();
			this.setVisible(false);
		} else if (e.getActionCommand().equals("comboCambiado")) {
			if (comboMapas.getSelectedIndex() == comboMapas.getItemCount() - 1 && !expandido){
				this.remove(boton);
				this.setSize(this.getWidth(), this.getHeight() + 100);
				this.add(anchoL);
				this.add(ancho);
				this.add(altoL);
				this.add(alto);
				this.add(minasL);
				this.add(minas);
				this.add(boton);
				expandido = true;
			} else if (expandido){
				this.remove(anchoL);
				this.remove(altoL);
				this.remove(minasL);
				this.remove(ancho);
				this.remove(alto);
				this.remove(minas);
				this.setSize(this.getWidth(), this.getHeight() - 100);
				expandido = false;
			}
			this.paintAll(this.getGraphics());
		}

	}

	public VentanaDeConfiguracion(Appletinicio applet) {
		super();
		this.setVisible(false);
		this.applet = applet;
		this.setSize(330, 100);
		expandido = false;
		this.setLayout(new FlowLayout());
		this.add(new JLabel("Mapa: "));
		ancho = new JTextField(20);
		alto = new JTextField(20);
		minas = new JTextField(20);
		anchoL = new JLabel ("Ancho: ");
		altoL = new JLabel ("Alto: ");
		minasL = new JLabel ("Minas: ");
		String[] mapasExistentes = {"Ningun mapa (nivel principiante)", 
				"Ningun mapa (nivel medio)", "Ningun mapa (nivel dificil)",
				"Centro vacio (nivel medio)", "Letra H (nivel dificil)",
				"Personalizado..."};
		comboMapas = new JComboBox(mapasExistentes);
		comboMapas.setActionCommand("comboCambiado");
		comboMapas.addActionListener(this);
		this.add(comboMapas);
		boton = new JButton("Aceptar");
		boton.setActionCommand("aceptar");
		boton.addActionListener(this);
		this.add(boton);
		
	}
	
	


}
